package com.hrm.controller;

import com.hrm.entity.Notice;
import com.hrm.entity.User;
import com.hrm.module.Dto;
import com.hrm.service.wxd.NoticeService;
import com.hrm.utils.DtoUtil;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * @author Miller
 */
@Controller
@RequestMapping("/notice")

public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    /**
     * 查询全部公告
     * @param
     * @return
     */
    @RequestMapping("/list")
    public String getAllNot(@RequestParam(value = "title",required = false) String title, ModelMap map){
        Notice notice=new Notice();
        notice.setTitle(title);
        List<Notice> notices = noticeService.selectAll(notice);
        map.addAttribute("noticesList",notices);
        return "/notice/list";
    }

    /**
     * 跳转添加公告
     * @return
     */
    @RequestMapping("/add")
    public String add(){
        return "notice/add";
    }

    /**
     * 添加公告
     * @param notice
     * @return
     */
    @PostMapping("/addNotice")
    public String addNoticeInfo(Notice notice,HttpSession session){
        User user_session =(User) session.getAttribute("user_session");
        notice.setCuid(user_session.getId());
        int i=noticeService.addNoticeInfo(notice);
        System.out.println(i);
        return "notice/list";
    }

    /**
     * 删除公告
     * @param id
     * @return
     */
    @GetMapping("/deleteNotice")
    public Dto deleteNotice(@RequestParam("id") Integer id){
        Dto<T> dto=null;
        int i = noticeService.deleteNoticeInfo(id);
        if (i>0){
            dto = DtoUtil.returnSuccess("删除成功");
        }else {
            dto = DtoUtil.returnSuccess("删除失败");
        }
        return dto;
    }

    /**
     * 修改
     * @param id
     * @param notice
     * @param session
     * @return
     */
    @GetMapping("/updateNoticea")
    public ModelAndView  updateNoticea(Integer id,Notice notice,HttpSession session){
        ModelAndView view = new ModelAndView();
        view.addObject("id",id);
        view.setViewName("notice/update");
        return view;
    }


    @PostMapping("/updateNotice")
    public ModelAndView  updateNotice(Notice notice,HttpSession session){
        int i = noticeService.updateNoticeInfo(notice);
        ModelAndView view = new ModelAndView();
        if (i>0){
            view.setViewName("notice/list");
        }
        return view;
    }





}
